// pages/ShowTicket/ShowETicket/index.js
var that
var common = require("../../../public.js");
var QRcode = require('../../../utils/weapp-qrcode.js')
var qrcode
Page({
  data: {
    ticketsInfo: [],
    enterDate: '',
    active: 0,
    canvasId: 'canvas0',
    venuTrue:false,
  },
  onLoad: function (options) {
    that = this
    var pages = getCurrentPages();
    var pagerouter = pages[pages.length-2].route
    if(pagerouter=='pages/ScenicArea/NianCardOrder/index' || pagerouter== '/pages/ScenicArea/ScenicAreaOrderDetail/index'){
        this.setData({
          tab_name:"会员卡"
        })
    }else{
      this.setData({
        tab_name:""
      })
    }
    console.log(options)
    const { windowWidth } = wx.getSystemInfoSync()
    const canvasWidth = parseInt(windowWidth * 0.5)
    const canvasHeight = canvasWidth
    this.setData({
      canvasWidth,
      canvasHeight
    })

    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token,
          openid: res.data.openid,
          orderNo: options.orderno,
          venuTrue:options.venuTrue? options.venuTrue : false
        }, () => {
          that.initData();
        })
      }
    })
  },
  initData() {
    let tempJson = {
      userId: that.data.userInfo.id,
      token: that.data.token,
      orderNo: that.data.orderNo
    }

      common.post('/venueOrder/getMyVenueQrcode', tempJson, function (res) {
      if (res.status == 200) {
        console.log(res.data,"645488225555")
        that.setData({
          ticketsInfo: res.data,
          // enterDate: that.transTime(res.data.enterDate)
        }, _ => {
          // 由于是动态设置的tab，需要重新计算tab的样式
          that.selectComponent('#vanTabs').resize()
          that.createQrcode(res.data[0].enterCode, res.data[0])
        })
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  createQrcode(enterCode) {
    new QRcode(that.data.canvasId, {
      text:  !this.data.venuTrue ? 'scenic=' + enterCode : 'venu=' + enterCode,
      width: this.data.canvasWidth,
      height: this.data.canvasHeight,
    })
  },
  transTime(strDate) {
    if (common.isEmptyString(strDate)) {
      return ''
    }
    var dateStr = strDate.trim().split(" ")
    var strGMT = dateStr[0] + " " + dateStr[1] + " " + dateStr[2] + " " + dateStr[5] + " " + dateStr[3] + " GMT+0800";
    return common.dateFormat('yyyy-MM-dd', Date.parse(strGMT))
  },

  onChange(e) {
    that.setData({
      canvasId: 'canvas' + e.detail.index,
      active: e.detail.index
    }, _ => {
      setTimeout(_ => {
        that.createQrcode(that.data.ticketsInfo.codeList[e.detail.index].enterCode)
      }, 100)

    })

  }
})